package com.dycong.common.IO.netty.frame_coder;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;

/**
 * Created by dycong on 2017/4/17.
 */
public class ChannelInitializerServer extends ChannelInitializer<SocketChannel> {
    private static final int MAX_FRAME_LENGTH = 1024 * 1024;
    private static final int LENGTH_FIELD_LENGTH = 4;
    private static final int LENGTH_FIELD_OFFSET = 2;
    @Override
     public void initChannel(SocketChannel ch) throws Exception{
        ch.pipeline()
                .addLast(new MyLengthFieldBasedFrameDecoder(MAX_FRAME_LENGTH,LENGTH_FIELD_OFFSET,LENGTH_FIELD_LENGTH))
                // TODO: 2017/4/17 msg instanceof ByteBuf判断，不属于 ByteBuf实例 LineBasedFrameDecoder不生效
                .addLast(new LineBasedFrameDecoder(20))
                //// TODO: 2017/4/17 因为 StringDecoder-->StringDecoder extends MessageToMessageDecoder<ByteBuf>
                //// TODO: 2017/4/17 matcher为泛型 MyLengthFieldBasedFrameDecoder解码之后为Msg 不为ByteBuf matcher.match(msg)，返回true
                // TODO: 2017/4/17 直接out.add(msg);相当于 StringDecoder不起作用了
                .addLast(new StringDecoder())
                .addLast(new MyMessageToByteEncoder())
                .addLast(new EchoServerHandler());
    }
    // TODO: 2017/4/17 多个Decoder不怕类型冲突，高层抽象类 都有是否属于ByteBuf实例的判断，已经Decode成非ByteBuf的实例，之后Decoder不再处理!!!
}
